use 668-Marijuana-Opoioid, clear

capture mkdir Output 
log using "Output/finalproject", text replace

/* Setting up panel data on state ID and year */ 

xtset fips year

/* Graphing All States Overdose Rates */

twoway (line od year if fips!=8) (scatter od year if fips==8)

/* Colorado Overdose Rate versus Donor Pool Average */

preserve  
gen treat = (fips==8)
collapse (mean) od, by(treat year)
xtset treat year
xtline od, overlay xline(2013)
graph export "Output/a.png", replace
restore

/* Synthetic Control */

synth od od poverty(2006(1)2013) uninsured(2008(1)2013) white(2006(1)2013) ///
income(2006(1)2013) unemployment(2001(1)2013) rx(2006(1)2013) ///
male(2006(1)2013) ba(2006(1)2013) hs(2006(1)2013) ///
youngadult(2006(1)2013) adult(2006(1)2013) elder(2006(1)2013), ///
trunit(8) trperiod(2013) xperiod(2001(1)2013) allopt nested fig ///
keep(stats)
graph export "Output/b.png", replace

/* Placebo in Time */

ynth od od poverty(2006(1)2013) uninsured(2008(1)2013) white(2006(1)2013) ///
income(2006(1)2013) unemployment(2001(1)2013) rx(2006(1)2013) ///
male(2006(1)2013) ba(2006(1)2013) hs(2006(1)2013) ///
youngadult(2006(1)2013) adult(2006(1)2013) elder(2006(1)2013), ///
trunit(8) trperiod(2007) ///
xperiod(2001(1)2007) resultsperiod(2001(1)2013) allopt nested fig

/* Placebo in Space */

synth_runner od od poverty(2006(1)2013) uninsured(2008(1)2013) ///
white(2006(1)2013) income(2006(1)2013) unemployment(2001(1)2013) ///
rx(2006(1)2013) male(2006(1)2013) ba(2006(1)2013) hs(2006(1)2013) ///
youngadult(2006(1)2013) adult(2006(1)2013) elder(2006(1)2013), trunit(8) ///
trperiod(2013) gen_vars allopt nested
single_treatment_graphs
 
single_treatment_graphs
graph export "Output/placeboinspaceproject_outcomes.png", replace name(raw)
graph export "Output/placeboinspaceproject_placebos.png", replace name(effects)

/* RMSPE Bar Graph */

gen rmspe_ratio = post_rmspe/pre_rmspe
tabstat rmspe_ratio, by(fips)
graph hbar rmspe_ratio, over(fips, sort(1) descending) ///
  ytitle("RMSPE ratio (Post-Treatment/Pre-Treatment)")
graph export "Output/placeboinspaceproject_fitratios_hbar.png", replace

/* RMSPE Plot, Adjusted */

summ pre_rmspe if fips==8
scalar COfit = r(mean)

gen relfit = pre_rmspe/COfit

keep if relfit<=2  
single_treatment_graphs
graph export "Output/placeboinspaceproject_placebos_rmspe2.png", replace name(effects)

